﻿Imports System.ComponentModel.Composition
Imports Foobar2000
Imports Foobar2000Helper
Imports System.Collections.Generic
Imports Send2Twitter

<ExportCompent(GetType(IInfoProvider), "Foobar2000 With Ax", "281f71f6-aeba-47bb-9a9e-152880c03d4b", "1.0.0.1")>
Public Class Foobar2kAxAddin
    Inherits InfoProviderBase

    Protected Foobar2kApp As Application07
    Protected WithEvents Foobar2kHelper As ApplicationHelper07

#Region "Private Members"
    Protected Sub CheckFoobar()
        If Foobar2kApp IsNot Nothing Then Return
        If Process.GetProcessesByName("foobar").Length <> 0 Then
            '进程存在，尝试建立COM对象
            Try
                Foobar2kApp = New Application07()
            Catch ex As Exception
                Foobar2kApp = Nothing
            End Try
        End If
    End Sub

#End Region

    Public Overrides Function GetAvailability() As Boolean
        If Foobar2kHelper.Running Then
            If Foobar2kApp Is Nothing Then
                Foobar2kApp = Foobar2kHelper.Server
            End If
            Return Foobar2kApp.Playback.IsPlaying
        Else
            Foobar2kApp = Nothing
            Return False
        End If
    End Function

    Public Overrides Function GetInfo() As System.Collections.Generic.Dictionary(Of String, String)
        Dim dic As New Dictionary(Of String, String)
        On Error Resume Next
        'TODO:大部分格式字符串不能正常工作需要调整
        dic.Add("album", Foobar2kApp.Playback.FormatTitle("%album%"))
        dic.Add("albumartist", Foobar2kApp.Playback.FormatTitle("%album artist%"))
        dic.Add("artist", Foobar2kApp.Playback.FormatTitle("%artist%"))
        dic.Add("disc", Foobar2kApp.Playback.FormatTitle("%discnumber%"))
        dic.Add("title", Foobar2kApp.Playback.FormatTitle("%title%"))
        dic.Add("track", Foobar2kApp.Playback.FormatTitle("%tracknumber%"))
        dic.Add("bitrate", Foobar2kApp.Playback.FormatTitle("%bitrate%"))
        dic.Add("filetype", Foobar2kApp.Playback.FormatTitle("%codec%"))
        dic.Add("samplerate", Foobar2kApp.Playback.FormatTitle("%samplerate%"))
        dic.Add("channels", Foobar2kApp.Playback.FormatTitle("%channels%"))
        dic.Add("trackposition", Foobar2kApp.Playback.FormatTitle("%playback_time_seconds%"))
        dic.Add("tracktime", Foobar2kApp.Playback.FormatTitle("%length_seconds%"))
        On Error GoTo 0
        Return dic
    End Function

    Public Overrides Sub ShutDown()

    End Sub

    Public Overrides Sub StartUp()
        Foobar2kHelper = New ApplicationHelper07
        GetAvailability()
    End Sub

    Private Sub Foobar2kHelper_ServerStateChanged(bRunning As Boolean) Handles Foobar2kHelper.ServerStateChanged

    End Sub
End Class
